package com.ww.core.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.ww.core.R;

public class ListRadioDialog {
	Context context;
	Dialog dlg;

	ListView listview;
	TextView title;
	Button cancel;
	RadioAdapter radioAdapter;

	public ListRadioDialog(Context context) {
		this.context = context;
		dlg = new Dialog(context, R.style.MyDialog);
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_radio);
		listview = (ListView) window.findViewById(R.id.listView);
		cancel = (Button) window.findViewById(R.id.cancel_btn);
		title = (TextView) window.findViewById(R.id.title);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();
			}
		});

	}

	public void show(String[] items, OnItemClickListener itemClickListener) {
		radioAdapter = new RadioAdapter(context, items);
		listview.setAdapter(radioAdapter);
		dlg.show();

		listview.setOnItemClickListener(itemClickListener);
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}

	class RadioAdapter extends BaseAdapter {
		private Context context;
		private String[] list;

		public RadioAdapter(Context context, String[] list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.length;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			TextView textView = new TextView(context);
			textView.setText(list[position]);
			textView.setHeight(80);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setTextSize(18);
			textView.setPadding(10, 0, 0, 0);
			textView.setTextColor(context.getResources().getColor(R.color.listradio_item));
			textView.setBackgroundResource(R.drawable.mylist_item);
			return textView;
		}
	}
}